<?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Goods extends Model
    {
        protected $fillable = ['name', 'brief', 'pictures', 'details', 'price', 'is_hot', 'classify_id'];
        protected $casts = [
            'classify_id' => 'array',
            'pictures' => 'array',
        ];
        
        //根据商品的classify_id找商品的分类数据
        public function getClassify($id)
        {

//        dd($id);
            $classify = Classify::where('id', $id)->get();
            $itme = [];
            foreach ($id as $value) {
                $itme[] = Classify::where('id', $value)->value('name');
            }
            return json_encode($itme, JSON_UNESCAPED_UNICODE);
        }
        
        //根据商品的id找商品的可用属性
        public function getAttrebute($id)
        {
//        dump($id);
            $attrebute = AttributeGoods::where('goods_id', $id)->pluck('attribute_id')->toArray();
            
            $attributes = [];
            foreach ($attrebute as $value) {
                $attributes[] = Attribute::where('id', $value)->get()->toArray()[0];
            }
            
        return $attributes;
        }
       //根据商品找货品的二级属性
        public function secondAttr($id){
            // 从商品属性中间表中获取当前商品的可用顶级属性id
            $topAttrsId = AttributeGoods::where('goods_id', $id)->pluck('attribute_id');
            // 获取可用顶级属性的数据
            $topAttrs = Attribute::whereIn('id',$topAttrsId)->get();
            // 从货品表中找到当前商品的所有货品的属性组合数据
            $huopin = Product::where('goods_id',$id)->pluck('attributeArr');
            // 循环所有货品的属性组合,然后合并去重,得到当前商品对应货品的可用子属性id数组
            // 定义一个空数组,用来接收货品的属性组合数组合并后的至
            $attributesArr = [];
            foreach ($huopin as $value){
                $attributesArr = array_unique(array_merge($attributesArr,$value));
            }
            // 循环当前商品的可用顶级属性,然后用每个可用顶级属性找到属性表中的pid等于他们id,并且属性id在上面的数组中的子属性的值
            foreach ($topAttrs as $k => $topAttr){
                $topAttr['son'] = Attribute::where('pid',$topAttr['id'])->whereIn('id',$attributesArr)->get()->toArray();
            }
            return $topAttrs;
        }
//        通过商品的id找二级分类下的三级
    public function getThird($id){
        //获得他的分类id
        //二级分类的id
        $classifyId=Goods::where('id',$id)->value('classify_id')[1];
       $ids=Classify::where('pid',$classifyId)->get();
//       return json_encode($ids,JSON_UNESCAPED_UNICODE);
       return $ids;
        
    }
    //获取此商品同一个三级分类下的商品
        public function goodbroth($id){
            //商品对应的三级分类
            $classifyId=Goods::where('id',$id)->first();
           foreach ($classifyId['classify_id'] as $value){
           }
           $thirdId=$value;
//           获取所有的商品分类
            $class=Goods::get();
            $goodNew=[];
           foreach ($class as $v){
               if (in_array($thirdId,$v['classify_id'])){
                   $goodNew[]=$v;
               }
           }
           return $goodNew;
        }
        //通过商品id找商品对应的三级,二级和一级分类
        public function getClassifyName($id){
       //获取商品id后,找分类
            $classify=Goods::where('id',$id)->pluck('classify_id')->toArray()[0];
//            dd($classify);
            //获取分类id之后然后取他的分类对象
            $classifys=Classify::whereIn('id',[10,24,86])->get();
           return $classifys;
        }
        //商品和关注的关联关系
        public function collect(){
            return $this->belongsTo(Collect::class,'goods_id','id');
        }
        //根据商品和用户id,判断是否已经收藏
        public function is_collect($id){
      return $collect=Collect::where('user_id',auth()->id())->where('goods_id',$id)->first();
        }
        
    }
